<!--
 * @Descripttion: 
 * @version: 
 * @Author: kk
 * @Date: 2020-10-24 11:35:07
 * @LastEditors: kk
 * @LastEditTime: 2020-10-30 18:19:24
-->
<!--  -->
<template>
  <div class='Center'>
    <el-page-header
      @back="goBack"
      content="详情页面"
    >
    </el-page-header>

    <div class="block">
       <h2>每个用户限购一件</h2>
      <span class="demonstration">
        <h3>{{ itemKillInfo.name }}</h3>
      </span>
      <img class="image"
        src="../assets/goods.jpg"
      >
    </div>
    <el-button type="primary" @click="kill2()">立即购买</el-button>

  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import itemKill from '@/api/miaosha'

export default {
  //import引入的组件需要注入到对象中才能使用

  components: {},
  data() {
    //这里存放数据
    return {
      itemKillId:{},
      itemKillInfo: {},
      userId: 10,
      url:"https://source.unsplash.com/random",
      md5:'',
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    goBack() {
      //console.log('go back');
      this.$router.go(-1);
    },
    getMd(){
        itemKill.getMd5(this.itemKillId,this.userId).then(response=>{
          if(response.code==200){
            this.md5=response.data.md5;
            console.log(this.md5)
          }else{
            this.$message({
                message: response.message,
                type: 'error'
          });
          }
        }).catch(error=>{
          this.$message({
                message: response.message,
                type: 'error'
          });
        })
    },
    getItemKill(){
        itemKill.getItemKillAndItem(this.itemKillId).then(response=>{
            this.itemKillInfo=response.data.itemKill
        }).catch(error=>{
          this.$message({
                message: response.message,
                type: 'error'
                });
        })
    },
    kill1(){
        itemKill.kill1(this.itemKillId,this.userId).then(response=>{
            const res=response
            if(res.code==200){
                this.$message({
                message: res.message,
                type: 'success'
                });
            }else{
                this.$message({
                message: res.message,
                type: 'error'
                });
                this.goBack()
            }
        })
    },
    kill2(){
        itemKill.kill2(this.itemKillId,this.userId,this.md5).then(response=>{
            const res=response
            if(res.code==200){
                this.$message({
                message: res.message,
                type: 'success'
                });
                //console.log(reponse.data.uuid)
                //this.$store.commit("SET_UUID", res.data.uuid);
                this.$router.push('/') 
            }else{
                this.$message({
                message: res.message,
                type: 'error'
                });
                this.goBack()
            }
        })
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.itemKillId=this.$route.query.id
    this.userId=this.$store.getters.getUser.id
    this.getItemKill()
    console.log(this.itemKillId)
    console.log( this.userId)
    this.getMd()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
.Center{
   text-align: center;
  margin: 0 auto;
}
.image{
  width: 30%;
  height: 40%;
}
</style>